package com.ipan.weixin.msg.general;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.Element;

/**
 * 地理位置消息
 * 
 * @author iPan
 * @version 2013-12-23
 */
public class LocationMsg extends PlainMsg {

	/** serialVersionUID */
	private static final long serialVersionUID = -4591162173408970545L;
	
	/** 地理位置维度X */
	private String locationX;
	
	/** 地理位置维度Y  */
	private String locationY;
	
	/** 地图缩放大小 */
	private String scale;
	
	/** 地理位置信息 */
	private String lable;
	
	public LocationMsg() {
		super(TransportType.RECEIVE);
	}
	
	public LocationMsg(Document doc) {
		this();
		fromXml(doc);
	}

	public String getLocationX() {
		return locationX;
	}

	public void setLocationX(String locationX) {
		this.locationX = locationX;
	}

	public String getLocationY() {
		return locationY;
	}

	public void setLocationY(String locationY) {
		this.locationY = locationY;
	}

	public String getScale() {
		return scale;
	}

	public void setScale(String scale) {
		this.scale = scale;
	}

	public String getLable() {
		return lable;
	}

	public void setLable(String lable) {
		this.lable = lable;
	}

	@Override
	public void fromXml(Document doc) {
		if (doc == null) {
			return ;
		}
		
		super.fromXml(doc);
		Element root = doc.getRootElement();
		this.locationX = root.elementTextTrim(EL_LOCATION_X);
		this.locationY = root.elementTextTrim(EL_LOCATION_Y);
		this.scale = root.elementTextTrim(EL_SCALE);
		this.lable = root.elementTextTrim(EL_LABEL);
	}

	@Override
	public String toXml(boolean nonEmpty) {
		StringBuilder buf = new StringBuilder("<xml>");
		buf.append(super.toXml(nonEmpty));
		buf.append(wrapElementHead(EL_MSG_TYPE)).append(TYPE_LOCATION).append(wrapElementEnd(EL_MSG_TYPE));
		if (nonEmpty) {
			if (StringUtils.isNotEmpty(this.locationX)) {
				buf.append(wrapElementHead(EL_LOCATION_X)).append(getStringValue(this.locationX)).append(wrapElementEnd(EL_LOCATION_X));
			}
			if (StringUtils.isNotEmpty(this.locationY)) {
				buf.append(wrapElementHead(EL_LOCATION_Y)).append(getStringValue(this.locationY)).append(wrapElementEnd(EL_LOCATION_Y));
			}
			if (StringUtils.isNotEmpty(this.scale)) {
				buf.append(wrapElementHead(EL_SCALE)).append(getStringValue(this.scale)).append(wrapElementEnd(EL_SCALE));
			}
			if (StringUtils.isNotEmpty(this.lable)) {
				buf.append(wrapElementHead(EL_LABEL)).append(getStringValue(this.lable)).append(wrapElementEnd(EL_LABEL));
			}
		} else {
			buf.append(wrapElementHead(EL_LOCATION_X)).append(getStringValue(this.locationX)).append(wrapElementEnd(EL_LOCATION_X));
			buf.append(wrapElementHead(EL_LOCATION_Y)).append(getStringValue(this.locationY)).append(wrapElementEnd(EL_LOCATION_Y));
			buf.append(wrapElementHead(EL_SCALE)).append(getStringValue(this.scale)).append(wrapElementEnd(EL_SCALE));
			buf.append(wrapElementHead(EL_LABEL)).append(getStringValue(this.lable)).append(wrapElementEnd(EL_LABEL));
		}
		return buf.append("</xml>").toString();
	}
}

/*---------------------------------------------
接收：
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1351776360</CreateTime>
<MsgType><![CDATA[location]]></MsgType>
<Location_X>23.134521</Location_X>
<Location_Y>113.358803</Location_Y>
<Scale>20</Scale>
<Label><![CDATA[位置信息]]></Label>
<MsgId>1234567890123456</MsgId>
</xml>
--------------------------------------------*/